package research.sbookmarking.input.xml;

import java.util.*;
import org.afox.parsers.xml.*;
import org.afox.util.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class SettingHandler extends ReflectiveHandler {
	private String currentClass;
	private String classPath;
	private ArrayList parameters = null;
	private ArrayList methodParameters = null;
	private ArrayList commands = new ArrayList();

	public void startSetting(Attributes attrs)	{
		getMainHandler().setResult(commands);
	}

	public void startCommand(Attributes attrs){
		classPath = attrs.getValue("classpath");
		currentClass = attrs.getValue("class");
		parameters = new ArrayList();
	}

	public void endCommand() {
		try {
			commands.add(RFactory.newInstance(classPath+currentClass, parameters));
		}
		catch(Exception x){
			System.err.println("Error instantiating Command: " + classPath + currentClass +
			"(" + parameters + "): ");
			x.printStackTrace();
		}
	}

	public void startParam(Attributes attrs) {
		parameters.add(StringType.parse(attrs.getValue("value")));
	}
	
	public void startMParam(Attributes attrs) {
		methodParameters.add(StringType.parse(attrs.getValue("value")));
	}

	public void startMethod(Attributes attrs) {
		methodParameters = new ArrayList();
	}

	public void endMethod() {
		parameters.add(methodParameters);
	}
}
